﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Threading;
using OtechWebServer.CommClass;

namespace OtechWebServer
{
    public class Global : System.Web.HttpApplication
    {
        
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            Thread t = new Thread(ClearCache);
            t.IsBackground = true;
            t.Start();
        }

        void Application_End(object sender, EventArgs e)
        {
            //  在应用程序关闭时运行的代码

        }

        void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码

        }

        void Session_Start(object sender, EventArgs e)
        {
            // 在新会话启动时运行的代码

        }

        void Session_End(object sender, EventArgs e)
        {
            // 在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时，才会引发 Session_End 事件。如果会话模式设置为 StateServer 
            // 或 SQLServer，则不会引发该事件。

        }

        private void ClearCache()
        {
            int startMinute = DateTime.Now.TimeOfDay.Minutes+1;
            Thread.Sleep((60-startMinute)*60*1000);
            while (true)
            {                
                Application.Lock();
                foreach (string company in Application.AllKeys)
                {
                    List<TaxiRouteInfo> list = Application[company] as List<TaxiRouteInfo>;
                    if (list != null)
                    {
                        ListToDatatable(company, list);
                    }
                }
                Application.UnLock();
                Thread.Sleep(60 * 60 * 1000);
            }            
        }


        public void ListToDatatable(string company, List<TaxiRouteInfo> list)
        {

            string path = "D:\\TaxiServer_Excel";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\TaxiRouteHistory";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\" + company;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\" + DateTime.Today.ToString("yyyy_MM_dd");
            path += ".xls";

            ExcelClass excelClass;
            if (!File.Exists(path))
            {
                excelClass = new ExcelClass();
            }
            else
            {
                excelClass = new ExcelClass(path);
            }
            DataTable dt = new DataTable();
            dt.Columns.Add("手机号码");
            dt.Columns.Add("车牌号码");
            dt.Columns.Add("司机姓名");
            dt.Columns.Add("起始时间");
            dt.Columns.Add("结束时间");
            dt.Columns.Add("上报服务器时间");
            dt.Columns.Add("行驶里程");
            dt.Columns.Add("状态");
            foreach (TaxiRouteInfo routeInfo in list)
            {
                DataRow row = dt.NewRow();
                row["手机号码"] = routeInfo.PhoneNumber;
                row["车牌号码"] = routeInfo.TaxiNo;
                row["司机姓名"] = routeInfo.Driver;
                row["起始时间"] = routeInfo.StartTime;
                row["结束时间"] = routeInfo.EndTime;
                row["上报服务器时间"] = routeInfo.ServerTime;
                row["行驶里程"] = routeInfo.Distance;
                row["状态"] = routeInfo.State==0?"载客":"空载";
                dt.Rows.Add(row);
            }
            excelClass.AppendData(dt);
            excelClass.Save(path);
        }


       


    }
}
